<template>
  <div style="padding: 10px;">
    <Form label-position="top">
      <Row>
        <Col span="24">
          <Row>
            <Col span="4">
              <FormItem style="margin-bottom: 14px;">
                <Input style="width: auto;" v-model="listParam.name" placeholder="请输入名称" />
              </FormItem>
            </Col>
            <Col span="4">
              <FormItem style="margin-bottom: 14px;">
                <Input style="width: auto;" v-model="listParam.username" placeholder="请输入用户名" />
              </FormItem>
            </Col>
            <Col span="10">
              <FormItem style="margin-bottom: 14px;">
                <DatePicker
                  type="date"
                  @on-change="getStartTime"
                  :value="listParam.start"
                  placeholder="开始时间"
                ></DatePicker>-
                <DatePicker
                  type="date"
                  @on-change="getEndTime"
                  :value="listParam.end"
                  placeholder="结束时间"
                ></DatePicker>
              </FormItem>
            </Col>
          </Row>
        </Col>
        <Col span="24" >
          <Row>
            <Col span="5">
              <FormItem >
                <Input style="width: auto;" v-model="listParam.content" placeholder="请输入操作内容" />
              </FormItem>
            </Col>
            <Col span="19" style="text-align: right;margin-bottom: 10px">
              <Button
                type="primary"
                style="margin-right: 15px"
                @click="searchAction"
                v-if="searchBtnShow"
              >搜索</Button>
              <!--<Button type="warning" @click="resetParam">重置</Button>-->
            </Col>
          </Row>

        </Col>

      </Row>
    </Form>
    <Table border ref="selection" :columns="columns" :data="logList">
      <template slot-scope="{ row, index }" slot="action">
        <span
          title="详情"
          class="button-class"
          v-if="row.type < 5 && detailBtnShow"
          type="text"
          @click="addBtn(row)"
        >详情</span>
        <span v-else>--</span>
      </template>
    </Table>
    <Page
      class="pagination"
      :page-size-opts="[10,20,50,100]"
      :total="page.total"
      :current="page.current"
      :page-size="page.pageSize"
      @on-change="pageChange"
      @on-page-size-change="sizeChange"
      show-elevator
      show-sizer
      show-total
    />
  </div>
</template>

<script>
export default {
  name: 'OperationLog',
  data() {
    return {
      detailBtnShow: false,
      searchBtnShow: false,
      listParam: {
        name: '',
        username: '',
        start: '',
        end: '',
        content: ''
      },
      logList: [],
      columns: [
        {
          title: '序号',
          align: 'center',
          width: 70,
          render: (h, params) => {
            return h(
              'span',
              params.index + (this.page.current - 1) * this.page.pageSize + 1
            )
          }
        },
        {
          title: '姓名',
          key: 'name',
          width: 150,
          align: 'center'
        },
        {
          title: '用户名',
          key: 'username',
          width: 150,
          align: 'center'
        },
        {
          title: '操作时间',
          key: 'operationTime',
          width: 250,
          align: 'center'
        },
        {
          title: '操作内容',
          key: 'content',
          align: 'center'
        },
        {
          title: '操作',
          width: 150,
          align: 'center',
          slot: 'action'
        }
      ],
      page: {
        total: 0,
        current: 1,
        pageSize: 10,
        totalPage: 0
      }
    }
  },
  methods: {
    //搜索
    searchAction() {
      this.page.current = 1
      this.getOperationLogList()
    },
    //重置
    resetParam() {
      this.listParam = {
        name: '',
        username: '',
        start: '',
        end: '',
        content: ''
      }
      this.getOperationLogList()
    },
    getStartTime(starTime) {
      console.log(starTime)
      this.listParam.start = starTime
    },
    getEndTime(endTime) {
      this.listParam.end = endTime
    },
    getOperationLogList() {
      let params = {
        currentPage: this.page.current,
        pageSize: this.page.pageSize,
        orderColumn: 'id',
        orderSort: 'desc',
        whereMap: {
          name: this.listParam.name, //"姓名",
          username: this.listParam.username, // "用户名",
          start: this.listParam.start, //"开始时间",
          end: this.listParam.end, //"结束时间",
          content: this.listParam.content //"操作内容"
        }
      }
      this.$api.operationLog.getOperationLogList(params).then(res => {
        if (res.code === 200) {
          this.logList = res.data.resultObject
          this.page.total = res.data.totalRecord
          this.page.totalPage = res.data.totalPage
        } else {
          this.$Message.error(res.message)
        }
      })
    },
    addBtn(row) {
      if (row.path) {
        let url = {
          params: { type: 'details', id: row.id,objectId:row.id, name: '日志详情' }
        }
        if (row.path === 'project-management/project-team/') {
          if (row.requestUri.indexOf('Workpoint') > 0) {
            url.name = 'project-team-work-detail'
          } else if (row.requestUri.indexOf('ProjectTeam') > 0) {
            url.name = 'project-team-org-detail'
          } else {
            url.name = 'project-team-dep-detail'
          }
        } else if (row.path === 'organization/enterprise-info/') {
          if (row.requestUri.indexOf('Area') > 0) {
            url.name = 'area-detail';
            url.params.type = 'detail';
            url.params.parentId = '1';
            url.params.parentName = '1';
          } else if (row.requestUri.indexOf('v1/Enterprise/') > 0) {
            url.name = 'company-detail';
            url.params.type = 'detail';
            url.params.parentId = '1';
            url.params.parentName = '1';
          } else if (row.requestUri.indexOf('v1/EnterpriseDept/') > 0) {
            url.name = 'department-detail';
            url.params.type = 'detail';
            url.params.parentId = '1';
            url.params.parentName = '1';
          }
        } else {
          url = { path: `/${row.path}${'details'}/${row.id}/${'日志详情'}` }
        }
        this.$router.push(url)
      } else {
        this.$Message.warning('缺少path')
      }
    },
    pageChange(current) {
      this.page.current = current
      this.getOperationLogList()
    },
    sizeChange(size) {
      this.page.current = 1
      this.page.pageSize = size
      this.getOperationLogList()
    },
    //按钮显示
    buttonShow() {
      let buttonList = JSON.parse(localStorage.getItem('buttonList'))
      buttonList.map(val => {
        let key = Object.keys(val).toString()
        if (key === this.$route.name) {
          Object.values(val)[0].map(item => {
            if (item.code === 'P_0506_01') {
              this.searchBtnShow = true
            } else {
              this.detailBtnShow = true
            }
          })
        }
      })
    }
  },
  mounted() {
    this.getOperationLogList()
    this.buttonShow()
  }
}
</script>

<style></style>
